package com.mastercard.labs

import com.sun.org.apache.bcel.internal.generic.NEW;

class MerchantService {
	def searchMerchantByCondition(name, country, category, status){
		def result = Merchant.createCriteria().list {
			if(category){
				createAlias("categories", "cat")
				eq("cat.id", Long.valueOf(category))
			}
			
			if(name){
				ilike("name", "%"+name+"%")
			}
			if(country){
				createAlias("country", "ctry")
				eq("ctry.id", Long.valueOf(country))
			}
			if(status){
				eq("status", Merchant.Status.valueOf(status))
			}
			ne("deleled", Boolean.valueOf(true))
		}
		return result
	}
	
	def searchMerchantForDashboard(){
		def result = Merchant.createCriteria().list {
			eq("deleled", Boolean.valueOf(false))
			order("name", "desc")
			maxResults(5)
		}
		return result
	}
	
	def getMerchantById(id){
		return Merchant.findById(id)
	}
	
	def saveMerchant(merchant){
		merchant.save()
	}
}
